<?php


namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\Area;
use app\common\model\xilujob\CompanyProfile;
use think\Db;
use think\Exception;
use function fast\array_get;


/**
 * 轮播图
 */
class CompanyAddress extends XilujobApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    protected $company;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->company = CompanyProfile::get(['user_id'=>$this->auth->id]);
    }


    /**
     * 添加地址
     */
    public function add_address() {
        $params = $this->request->post('',null);
        $params['user_id'] = $this->auth->id;
        $params['company_id'] = $this->company->id;

        if($params['address']){
            $result = xilujobGetAddr($params['lat'],$params['lng']);
            if($result){
                $params['province'] = $result['prov'];
                $params['city'] = $result['city'];
                $params['district'] = $result['district'];
            }else{
                $this->error("请检查地图配置");
            }
        }
        //在数据库中找省市区
        $params['province_id'] = array_get($params,'province_id',0);
        if($province = array_get($params,'province')){
            $params['province_id'] = Area::getIdByName($province,1,0);
        }
        $city = array_get($params,'city');
        $params['city_id'] = array_get($params,'city_id',0);
        if($params['province_id'] && $city){
            $params['city_id'] = Area::getIdByName($city,2,$params['province_id']);
        }
        $district = array_get($params,'district');
        $params['district_id'] = array_get($params,'district_id',0);
        if($params['city_id'] && $district){
            $params['district_id'] = Area::getIdByName($district,3,$params['city_id']);
        }
        Db::startTrans();
        try {
            $ret = \app\common\model\xilujob\CompanyAddress::addAddress($params);
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('保存成功', $ret->relationQuery([
            'province'=>function($query){$query->field('id,name');},
            'city'=>function($query){$query->field('id,name');},
            'district'=>function($query){$query->field('id,name');},
        ]));

    }

    public function index() {

        $lists = \app\common\model\xilujob\CompanyAddress::with([
            'province'=>function($query){$query->field('id,name');},
            'city'=>function($query){$query->field('id,name');},
            'district'=>function($query){$query->field('id,name');},

        ])->where('company_id', $this->company->id)
            ->order('id','desc')
            ->select();
        $this->success('',$lists);
    }


    /**
     * 删除
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del() {
        $id = $this->request->post('id');
        $row = $id ? \app\common\model\xilujob\CompanyAddress::where('id',$id)
            ->find() : null;
        if(!$row){
            $this->error("地址不存在");
        }
        #关联的地址
        $job = \app\common\model\xilujob\Job::alias("job")
            ->join("xilujob_job_location job_location",'job_location.job_id=job.id')
            ->where('job_location.company_address_id',$row->id)
            ->count(1);
        if($job){
            $this->error("有关联职位");
        }
        $row->delete();
        $this->success('删除成功',$row);
    }

}